package com.image.utils;

import com.util.DBUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/7/2 22:23
 * @since JDK 1.8
 *
 * 图片存入数据库处理
 */
public class DBImageUtils {

    // 将图片插入数据库
    public static void insertImageToDB(File file,Integer fid) {
        Connection conn = DBUtils.getConnection();
        PreparedStatement ps = null;
        FileInputStream in = null;
        try {
            in = readImage(file.getPath());
            String sql = "insert into m_image (id,file) values(?,?) ";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,fid);
            ps.setBinaryStream(2, in, in.available());
            int count = ps.executeUpdate();
            if (count > 0) {
                System.out.println("插入成功！");
            } else {
                System.out.println("插入失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != ps) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 读取本地图片获取输入流
    public static FileInputStream readImage(String path) throws IOException {
        return new FileInputStream(new File(path));
    }

    // 读取数据库中图片
    public static InputStream  selectFileById(Integer fid) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        InputStream in=null;
        try {
            conn = DBUtils.getConnection();
            String sql = "select * from m_image where id=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,fid);
            rs = ps.executeQuery();
            while (rs.next()) {
                in = rs.getBinaryStream("file");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

        }
        return in;
    }


    // 读取表中图片获取输出流
    public static void readBin2Image(InputStream in, String targetPath) {
        File file = new File(targetPath);
        String path = targetPath.substring(0, targetPath.lastIndexOf("/"));
        if (!file.exists()) {
            new File(path).mkdir();
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = in.read(buf)) != -1) {
                fos.write(buf, 0, len);
            }
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 读取表中图片获取输出流
    public static void readBin2Image2(InputStream in, HttpServletResponse response) {
        ServletOutputStream fos = null;
        try {
            fos = response.getOutputStream();
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = in.read(buf)) != -1) {
                fos.write(buf, 0, len);
            }
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
